# Copyright (c) 2009 by Alex Leone <acleone ~at~ gmail.com>
#
# This file is part of epp.
#
# epp is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# epp is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with epp.  If not, see <http://www.gnu.org/licenses/>.

r"""
Contains the [strftime] tag.

Syntax:
    [strftime] (= [time])
    [strftime f="{strftime format string}"]

Output:
    Outputs the results of a call to datetime's strftime.
    See http://docs.python.org/library/time.html#time.strftime

Examples:
    Note: s[1:] in p.parsetags(s[1:]) is to strip the leading '\n'
          from s

    >>> import epp
    >>> p = epp.Epp()
    >>> print(p.parsetags('[strftime f="%%"]'))
    %
"""

import datetime
import tags
from tags.tag import Tag

tagnames = ["strftime", "time"]

class Strftime:

    def replace(self, eppinst, tagName, attrs):
        """
        See the python strftime function.
        Returns: the current time formated
        """
        now = datetime.datetime.now()
        if len(attrs) == 0:
            return str(now)
        n, format = attrs[0]
        return now.strftime(format)